package ui.panels;

import entities.ViewScheduler;
import java.awt.Color;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JOptionPane;
import manager.GenericDriver;
import manager.ManagerDriver;
import model.ViewShiftComboBoxModel;
import ui.windows.RemedySchedulerFrame;

public class EditHeadSchedulerPanel extends javax.swing.JPanel {

    /** Creates new form EditHeadSchedulerPanel */
    public EditHeadSchedulerPanel(RemedySchedulerFrame mainFrame, HeadSchedulerManagementPanel parentPanel) {
        this.mainFrame = mainFrame;
        this.parentPanel = parentPanel;
        driver = (ManagerDriver) mainFrame.getDriver();
        genDriver = mainFrame.getGenericDriver();
        initComponents();
        includeModel();
    }

    private void includeModel() {
        checkDate();
    }

    private void updateRecord() {
        checkDate();
        if (checkDate) {
            Date endDate = dcEndDate.getDate();
            selectedRecord.setEndDate(endDate);
            boolean returnVal = parentPanel.updateScheduler(selectedRecord);
            if (returnVal) {
                close();
                JOptionPane.showMessageDialog(null, "Updated");
            } else {
                JOptionPane.showMessageDialog(null, "Cannot update !", "Error", JOptionPane.WARNING_MESSAGE);
            }
        }
    }

    private void checkDate() {
        Date endDate = dcEndDate.getDate();
        if (endDate != null && endDate.before(selectedRecord.getStartDate())) {
            lbCheckDate.setText("invalid !");
            lbCheckDate.setForeground(Color.red);
            checkDate = false;
        } else {
            if (selectedRecord != null) {
                if ((endDate != null && selectedRecord.getEndDate() != null && dateFormat.format(endDate).equals(dateFormat.format(selectedRecord.getEndDate()))) || (endDate == null && selectedRecord.getEndDate() == null)) {
                    checkDate = false;
                    new Thread() {

                        @Override
                        public void run() {
                            JOptionPane.showMessageDialog(mainFrame, "You not assign !");
                        }
                    }.start();
                } else {
                    lbCheckDate.setText("");
                    checkDate = true;
                }
            }
        }
    }

    public void setSelectedRecord(ViewScheduler record) {
        this.selectedRecord = record;
        if (selectedRecord != null) {
            lbID.setText(selectedRecord.getLoginID() + "");
            lbDep.setText(selectedRecord.getDepartmentName());
            lbShift.setText(selectedRecord.getShiftName());
            lbStartDate.setText(dateFormat.format(selectedRecord.getStartDate()));
            dcEndDate.setDate(selectedRecord.getEndDate());
        } else {
            lbID.setText("?");
            lbDep.setText("?");
            lbShift.setText("?");
            lbStartDate.setText("?");
            dcEndDate.setDate(null);
        }
    }

    private void clear() {
        lbID.setText("?");
        lbDep.setText("?");
        lbShift.setText("?");
        lbStartDate.setText("?");
        dcEndDate.setDate(null);
        lbCheckDate.setText("");
        lbShift.setText("?");
        selectedRecord = null;
    }

    private void close() {
        clear();
        parentPanel.closeTarget();
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    private RemedySchedulerFrame mainFrame;
    private HeadSchedulerManagementPanel parentPanel;
    private ManagerDriver driver;
    private GenericDriver genDriver;
    private ViewShiftComboBoxModel model;
    private ViewScheduler selectedRecord;
    private boolean checkDate;
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lbClose = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        lbID = new javax.swing.JLabel();
        lbDep = new javax.swing.JLabel();
        dcEndDate = new com.toedter.calendar.JDateChooser();
        lbShift = new javax.swing.JLabel();
        lbCheckDate = new javax.swing.JLabel();
        lbStartDate = new javax.swing.JLabel();
        bUpdate = new javax.swing.JButton();

        setBackground(new java.awt.Color(255, 255, 255));

        lbClose.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ui/images/close_20.png"))); // NOI18N
        lbClose.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                lbCloseMouseClicked(evt);
            }
        });

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));
        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(), "Head's Scheduler Details", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Agency FB", 1, 14))); // NOI18N

        jLabel2.setText("ID:");

        jLabel3.setText("Department:");

        jLabel4.setText("Shift:");

        jLabel5.setText("Start Date:");

        jLabel6.setText("End Date:");

        lbID.setFont(new java.awt.Font("Tahoma", 1, 11));
        lbID.setText("value");

        lbDep.setFont(new java.awt.Font("Tahoma", 1, 11));
        lbDep.setText("value");

        lbShift.setFont(new java.awt.Font("Tahoma", 1, 11));
        lbShift.setText("value");

        lbCheckDate.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        lbCheckDate.setText("value");

        lbStartDate.setFont(new java.awt.Font("Tahoma", 1, 11));
        lbStartDate.setText("value");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(22, 22, 22)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel6)
                    .addComponent(jLabel5)
                    .addComponent(jLabel4)
                    .addComponent(jLabel3)
                    .addComponent(jLabel2))
                .addGap(100, 100, 100)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(dcEndDate, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(lbID)
                            .addComponent(lbDep))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(lbCheckDate, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(lbShift)
                    .addComponent(lbStartDate))
                .addContainerGap(46, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(30, 30, 30)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(lbCheckDate)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(lbID))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3)
                            .addComponent(lbDep))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel4)
                            .addComponent(lbShift))
                        .addGap(12, 12, 12)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel5)
                            .addComponent(lbStartDate))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel6)
                            .addComponent(dcEndDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(32, Short.MAX_VALUE))
        );

        bUpdate.setText("Update");
        bUpdate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bUpdateActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(66, 66, 66)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(bUpdate, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 115, Short.MAX_VALUE)
                .addComponent(lbClose)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(lbClose))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(26, 26, 26)
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(bUpdate)
                .addContainerGap(152, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

private void bUpdateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bUpdateActionPerformed
    updateRecord();
}//GEN-LAST:event_bUpdateActionPerformed

private void lbCloseMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lbCloseMouseClicked
    close();
}//GEN-LAST:event_lbCloseMouseClicked
    private static SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/YYYY");
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bUpdate;
    private com.toedter.calendar.JDateChooser dcEndDate;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JLabel lbCheckDate;
    private javax.swing.JLabel lbClose;
    private javax.swing.JLabel lbDep;
    private javax.swing.JLabel lbID;
    private javax.swing.JLabel lbShift;
    private javax.swing.JLabel lbStartDate;
    // End of variables declaration//GEN-END:variables
}
